﻿using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.NetWork.Base;
using HR60_PanelSoft.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HR60_PanelSoft.NetWork.Sender
{
    class Sender_010B:BaseSender
    {

        public Sender_010B(byte panelNo,Panel panel)
        {

            bool isAir = false;
            byte airNum = 0;

            bool isHeat =false;
            byte heatNum = 0;
            byte pinHeatNum = 0;

            bool isWind =false;
            byte windNum = 0;


            List<LoadPin> loadPins = panel.loadPins;
            foreach (LoadPin lp in loadPins)
            {
                Device device = lp.getConnectedDevice();

                Device parentDevice = null;
                int  num=0;
                DoubleReuslt<Device, int> doubleResult = Device.findParentAndDoubleReuslt(device);
                if (doubleResult != null)
                {
                    parentDevice = doubleResult.Value1;
                    num = doubleResult.Value2;
                }



                if (parentDevice == null)
                    continue;


                if (parentDevice.type.exType == ExDeviceType.CentralAircon)
                {
                    isAir = true;
                    airNum = (byte)num;
                }
                else if (parentDevice.type.exType == ExDeviceType.CentralHeating)
                {
                    isHeat = true;
                    heatNum = (byte)num;
                    pinHeatNum = (byte)lp.index;
                }
                else if (parentDevice.type.exType == ExDeviceType.CentralNewWind)
                {
                    isWind = true;
                    windNum = (byte)num;
                }          
            }
            byte[] data = new byte[5];


            if (isAir && !isHeat && !isWind)
            {
                data[0] = 0x01;
                data[1] = airNum;
            }
            else if (!isAir && isHeat && !isWind)
            {
                data[0] = 0x02;
                data[2] = heatNum;
                data[3] = pinHeatNum;
            }
            else if (!isAir && !isHeat && isWind)
            {
                data[0] = 0x03;
                data[1] = windNum;
            }
            else if (isAir && isHeat && !isWind)
            {
                data[0] = 0x04;
                data[1] = airNum;
                data[2] = heatNum;
                data[3] = pinHeatNum;
            }
            else if (!isAir && isHeat && isWind)
            {
                data[0] = 0x05;
                data[1] = windNum;
                data[2] = heatNum;
                data[3] = pinHeatNum;
            }

            this.package = Package.getPackage(0x01, 0x0B, panelNo, 0x01, data);

        }
    }
}
